package cn.hchaojie.boot.convert;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

public class DateConverter implements Converter<String, Date> {
	private static final DateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	private static final DateFormat FORMAT2 = new SimpleDateFormat("yyyy-MM-dd");
	
	@Override
	public Date convert(String source) {
		if (source == null || source.length() == 0) {
			return null;
		}
	
		try {
			if (source.length() == 10) {
				return FORMAT2.parse(source);
			}
			
			if (source.length() == 19) {
				return FORMAT.parse(source);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		return null;
	}

}
